axios.defaults.baseURL = '../student/';
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
axios.defaults.transformRequest = function (data) {
    let transObj = '';
    for (let i in data) {
        transObj += i + '=' + data[i] + '&';
    }
    return transObj;
}
axios.interceptors.request.use(
    config => {
        NProgress.start() // 设置加载进度条(开始..)

        return config
    },
    error => {
        return Promise.reject(error)
    }
)
// axios响应拦截器
axios.interceptors.response.use(
    function(response) {
        NProgress.done() // 设置加载进度条(结束..)
        return response
    },
    function(error) {
        return Promise.reject(error)
    }
)
new Vue({
    el: '#dv',
    data: {
        showStudent: {
            size: 5,
            page: 1,
        },
        id: '',
        flag: false,
        total: 0,
        list: [],
        student: null
    },
    methods: {
        getAllList(mes) {
            axios.post('list.php', mes)
            //transformRequest就是用来处理转换的)
                .then(result => {
                    if (result.status === 200) {
                        let data = result.data.data.list
                        this.total = result.data.data.count
                        this.list = data
                    } else {
                        alert("服务器出错")
                    }
                })
        },
        add(obj) {
            axios.post('add.php',obj)
                .then(result => {
                    if (result.status === 200) {
                        alert("添加成功")
                        this.getAllList(this.showStudent)
                    } else {
                        alert("服务器出错")
                    }

                })
        },
        del(id) {
            axios.post('delete.php', {id: id})
                .then(result => {
                    if (result.status === 200) {
                        alert("删除成功")
                        if(Math.ceil(parseInt(this.total - 1) / this.showStudent.size)==1){
                            this.getAllList(this.showStudent)
                        }
                    } else {
                        alert('服务器出错')
                    }
                })
        },
        update(obj) {
            axios.post('update.php',obj)
                .then(result => {
                    if (result.status === 200) {
                        this.flag = false
                        this.getAllList(this.showStudent)
                    } else {
                        alert("服务器出错")
                    }
                })
        },
        modify(id) {
            this.flag = true
            this.id = id
            axios.post('get.php', {id: id})
                .then(result => {
                    if (result.status === 200) {
                        this.student = result.data.data
                    } else {
                        alert("服务器出错")
                    }
                })
        },
        cancel(value){
            this.flag = value
        },
        next() {
            this.showStudent.page += 1
            if (this.showStudent.page > Math.ceil(parseInt(this.total) / this.showStudent.size)) {
                return
            }
            this.getAllList(this.showStudent)
        },
        prev() {
            this.showStudent.page -= 1
            if (this.showStudent.page < 1) {
                return
            }
            this.getAllList(this.showStudent)
        },
        choosePage(index) {
            this.showStudent.page = index
            this.getAllList(this.showStudent)
        },
    },
    components: {
        'std-header': {
            template: '#header',
            data() {
                return {
                    name: '',
                    sex: 1,
                    age: '',
                    edu: '',
                    city: 1,
                    no: 0,
                }
            },
            methods: {
                add(){
                   this.$emit('addfun',{
                       name: this.name,
                       sex: this.sex,
                       age: this.age,
                       edu: this.edu,
                       city: this.city,
                       no: this.no,
                   })
                    this.name = this.age = this.edu = this.no = ''
                }
            }
        },
        'std-main': {
            template: '#main',
            data() {
                return {}
            },
            props: {
                list: {
                    type: Array,
                    required: true
                },
                total: {
                    required: true
                },
                size: {
                    type: Number,
                    required: true
                }
            },
            methods: {
                modify(id) {
                    this.$emit('modifyfun', id)
                },
                del(id){
                    this.$emit('delfun',id)
                },
                prev(){
                    this.$emit('prevfun')
                },
                next() {
                    this.$emit('nextfun')
                },
                choosePage(index){
                    this.$emit('choosefun',index)
                }
            },
            filters: {
                isGender(value) {
                    return value == 1 ? '男' : '女'
                },
                cityFormat(city) {
                    city = parseInt(city)
                    switch (city) {
                        case 1:
                            return '北京'
                        case 2:
                            return '福州'
                        case 3:
                            return '厦门'
                        case 4:
                            return '龙岩'
                        case 5:
                            return '泉州'

                    }
                }
            },
            computed: {
                pages() {
                    return Math.ceil(parseInt(this.total) / this.size)
                }

            }
        },
        'std-update': {
            template: '#update',
            data() {
                return {
                    upname: '',
                    upsex: 1,
                    upage: '',
                    upedu: '',
                    upcity: 1,
                    upno: 0,
                    id: 0
                }
            },
            props: {
                student: {
                    type: Object
                }
            },
            methods: {
                cancel(){
                    this.$emit('cancelfun',false)
                },
                update(){
                    this.$emit('updatefun',{
                            id: this.id,
                            name: this.upname,
                            sex: this.upsex,
                            age: this.upage,
                            edu: this.upedu,
                            city: this.upcity,
                            no: this.upno,
                    })
                }
            },
            watch: {
                student(val){
                    this.upname = val.name
                    this.upsex = val.sex
                    this.upage = val.age
                    this.upedu = val.edu
                    this.upcity = val.city
                    this.upno = val.no
                    this.id = val.id
                }
            }
        }
    },
    created() {
        this.getAllList(this.showStudent)
    }
})
